package be.cgi.heroesmanager.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import be.cgi.heroesmanager.model.Hero;

public class HeroesAdapter {

	private Context context;

	public static final String DB_NAME = "HeroesManager";
	public static final String TABLE_HEROES = "Heroes";

	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_NAME = "Name";
	public static final String COLUMN_CITY = "City";
	public static final String COLUMN_ABILITY = "Ability";

	public static final int VERSION = 1;

	private HeroesHelper helper;
	private SQLiteDatabase db;

	public HeroesAdapter(Context activityContext) {
		this.context = activityContext;
	}

	public HeroesAdapter openReadable() {
		helper = new HeroesHelper(context, DB_NAME, null, VERSION);
		db = helper.getReadableDatabase();

		return this;
	}

	public HeroesAdapter openWritable() {
		helper = new HeroesHelper(context, DB_NAME, null, VERSION);
		db = helper.getWritableDatabase();

		return this;
	}

	public Cursor getCursor(String[] columns, String selection,
			String[] selectionArgs, String orderBy) {
		Cursor getHeroes = db.query(true, TABLE_HEROES, columns, selection,
				selectionArgs, null, null, orderBy, null);
		getHeroes.moveToFirst();

		return getHeroes;
	}

	public Cursor getAllCursor() {
		String[] columns = { COLUMN_ID, COLUMN_NAME, COLUMN_CITY,
				COLUMN_ABILITY };

		// Cursor allHeroes = db.query(true, TABLE_HEROES, columns,
		// null, null, null, null, COLUMN_NAME, null);
		// allHeroes.moveToFirst();
		//
		// return allHeroes;

		return getCursor(columns, null, null, COLUMN_NAME);
	}

	public List<Hero> getAll() {
		List<Hero> heroList = new ArrayList<Hero>();

		Cursor cursor = getAllCursor();

		do {
			heroList.add(cursorToHero(cursor));
		} while (cursor.moveToNext());

		return heroList;
	}

	public Cursor getOneCursor(long id) {
		String[] columns = { COLUMN_ID, COLUMN_NAME, COLUMN_CITY,
				COLUMN_ABILITY };

		return getCursor(columns, COLUMN_ID + " =?",
				new String[] { String.valueOf(id) }, COLUMN_NAME);
	}

	public Hero getOne(long id) {
		return cursorToHero(getOneCursor(id));
	}

	public long insert(Hero h) {
		ContentValues values = new ContentValues();

		values.put(COLUMN_NAME, h.getName());
		values.put(COLUMN_CITY, h.getCity());
		values.put(COLUMN_ABILITY, h.getAbility());

		return insert(values);
	}

	public long insert(ContentValues values) {
		return db.insert(TABLE_HEROES, null, values);
	}

	public Hero cursorToHero(Cursor c) {

		int id = c.getInt(c.getColumnIndex(COLUMN_ID));
		String name = c.getString(c.getColumnIndex(COLUMN_NAME));
		String city = c.getString(c.getColumnIndex(COLUMN_CITY));
		String ability = c.getString(c.getColumnIndex(COLUMN_ABILITY));

		Hero hero = new Hero(id, name, city, ability);

		return hero;
	}

	public void close() {
		db.close();
		helper.close();
	}
}